package com.lehe.controller.admin;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lehe.common.exceptions.LHAdminException;
import com.lehe.entry.BaseResponse;
import com.lehe.entry.dbbean.GoldDetailDto;
import com.lehe.service.impl.GoldRebateService;
import com.lehe.util.DateUtil;

@RequestMapping("admin/rebateDetail")
@Controller
public class GoldRebateController extends BaseAdminController{
	
	@Autowired
	private GoldRebateService goldDetailService;
	/**
	 * 财务管理-用户查询
	 */
	@RequestMapping
	public String userfinance(Model m){
		return "admin/goldRebate/goldRebate";
	}
	
	@RequestMapping("rebateList")
	public String rebateList(String dt,Model model){
		try {
			Map<String, Object> map = goldDetailService.goldRebate(dt);
			model.addAttribute("data",map);
		} catch (LHAdminException e) {
			model.addAttribute("msg",e.getLocalizedMessage());
			e.printStackTrace();
		}
		
		return "admin/goldRebate/goldRebateList";
	}
	
	@RequestMapping("doInsert")
	@ResponseBody
	public BaseResponse doInsert(String dt,Long automaticTrigger,Model model){
		BaseResponse resp = new BaseResponse(BaseResponse.SUCCESS,"");
		if(DateUtil.isToday(dt)){
			logger.error("计算提成入库异常,当天数据不允许入库！");
			resp.setCode(BaseResponse.FAIL);
			resp.setMsg("当天数据不允许入库！");
			return resp;
		}
		try {
			Map<String, Object> map = goldDetailService.queryGoldRebate(dt);
			model.addAttribute("data",map);
			//automaticTrigger  本次计算提成是不是job跑的 0否 1是	
			if(automaticTrigger==1l){
				goldDetailService.doInsert(map,automaticTrigger,null);
			}else{
				goldDetailService.doInsert(map,automaticTrigger,this.getAdminUserId());
			}
		} catch (LHAdminException e) {
			logger.error("计算提成入库异常", e);
			resp.setCode(BaseResponse.FAIL);
			resp.setMsg(e.getMessage());
			return resp;
		} catch (Exception e) {
			logger.error("计算提成入库异常", e);
			resp.setCode(BaseResponse.FAIL);
			resp.setMsg("计算提成入库失败");
			return resp;
		}

		return resp;
	}
	
}
